<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构简称" prop="shortName">
        <el-input
            v-model="queryParams.shortName"
            placeholder="请输入机构简称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status" style="width: 280px">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
              v-for="dict in institution_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付账号" prop="account">
        <el-input
            v-model="queryParams.account"
            placeholder="请输入支付账号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="institutionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机构ID" align="center" prop="id" />
      <el-table-column label="机构简称" align="center" prop="shortName" />
      <el-table-column label="logo" align="center" prop="logo" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.logo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="institution_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="官方邮箱" align="center" prop="officialEmail" />
      <el-table-column label="支付账号" align="center" prop="account" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['insititution:institution:edit']">查看</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['insititution:institution:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改机构对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="institutionRef" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="机构简称" label-width="100px">
          <el-input v-model="form.shortName" placeholder="请输入机构简称" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="机构全称" label-width="100px">
          <el-input v-model="form.fullName" placeholder="请输入机构全称" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="logo" label-width="100px">
          <MyImgUpload v-model="form.logo" disabled limit="1" />
        </el-form-item>
        <el-form-item label="机构地址" label-width="100px">
          <el-input v-model="form.address" placeholder="请输入机构地址" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="官方邮箱" label-width="100px">
          <el-input v-model="form.officialEmail" placeholder="请输入官方邮箱" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="法人姓名" label-width="100px">
          <el-input v-model="form.ipName" placeholder="请输入法人姓名" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="法人身份证号" label-width="100px">
          <el-input v-model="form.ipIdNo" placeholder="请输入法人身份证号" disabled  class="disabled-input"/>
        </el-form-item>
        <el-form-item label="法人身份证人像面图片" label-width="100px" >
          <MyImgUpload v-model="form.ipIdFrontImg" disabled limit="1" class="disabled-input"/>
        </el-form-item>
        <el-form-item label="法人身份证国徽面图片" label-width="100px" >
          <MyImgUpload v-model="form.ipIdBackImg" disabled limit="1" class="disabled-input"/>
        </el-form-item>
        <el-form-item label="营业执照号码" label-width="100px">
          <el-input v-model="form.blNumber" placeholder="请输入营业执照号码" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="统一社会信用代码" label-width="100px">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" disabled class="disabled-input"/>
        </el-form-item>
        <el-form-item label="营业执照" label-width="100px">
          <MyImgUpload v-model="form.blImg" disabled limit="1" class="disabled-input"/>
        </el-form-item>
        <el-form-item label="支付账号" label-width="100px">
          <el-input v-model="form.account" placeholder="请输入支付账号" disabled  class="disabled-input"/>
        </el-form-item>
        <el-form-item label="介绍" label-width="100px">
          <el-input v-model="form.introduction" placeholder="请输入介绍" disabled  class="disabled-input"/>
        </el-form-item>
        <!-- 审核状态表单项放在最下面 -->
        <el-form-item label="审核状态" label-width="100px">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in institution_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Institution">
import { listInstitution, getInstitution, delInstitution, addInstitution, updateInstitution } from "@/api/insititution/institution";
const { proxy } = getCurrentInstance();
const { institution_status } = proxy.useDict('institution_status');
import MyImgUpload from "@/components/MyImgUpload/index.vue";

const institutionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shortName: null,
    status: null,
    account: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询机构列表 */
function getList() {
  loading.value = true;
  listInstitution(queryParams.value).then(response => {
    institutionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    shortName: null,
    fullName: null,
    logo: null,
    status: null,
    address: null,
    officialEmail: null,
    ipName: null,
    ipIdNo: null,
    ipIdFrontImg: null,
    ipIdBackImg: null,
    blNumber: null,
    creditCode: null,
    blImg: null,
    account: null,
    introduction: null
  };
  proxy.resetForm("institutionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加机构";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInstitution(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "审批机构";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["institutionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInstitution(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInstitution(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除机构编号为"' + _ids + '"的数据项？').then(function() {
    return delInstitution(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('insititution/institution/export', {
    ...queryParams.value
  }, `institution_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style scoped>
.app-container {
  padding: 20px;
}

.el-dialog {
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-form-item__label {
  color: #606266;
  font-weight: 500;
}

.el-input__inner,
.el-textarea__inner {
  border-radius: 4px;
}

.el-button {
  border-radius: 4px;
}

.dialog-footer {
  text-align: right;
  margin-top: 20px;
}

.disabled-input .el-input__inner {
  background-color: #f5f7fa;
  color: #c0c4cc;
}

</style>